import axios from 'axios'
import {
  Toast
} from 'mint-ui';
import store from '@/store/index'
axios.defaults.withCredentials = true;
const service = axios.create({

  baseURL: process.env.NODE_ENV === "development" ? "api" : '', // 的 base_url process.env.VUE_APP_URL, 
  timeout: 10000 // request timeout
})


// request interceptor
service.interceptors.request.use(
  config => {

    return config
  },
  error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  }
)


// response interceptor
service.interceptors.response.use(function (response) {
    const res = response.data

    if (res.code === -1) {
      return Promise.reject(false)
    } else if (res.code !== "E00000") {
      console.log('response.config.url', response.config);
      //取消订单 关闭提示80S
      if (response.config.url.indexOf('/find/cancellationOfOrder') != -1) {
        store.dispatch('setOrderTime', null)
      }
      //发起支付 回调
      if (response.config.url.indexOf('/pay/wxPay') != -1 || response.config.url.indexOf('/pay/zfbzf') != -1) {
        return response.data
      } else {
        Toast({
          message: res.message,
          position: 'middle',
          duration: 5000
        });
        return Promise.reject(false)
      }
    } else {
      return response.data.data
    }
  },
  error => {
    Toast({
      message: error.message,
      position: 'middle',
      duration: 5000
    });
    return Promise.reject(error)
  }
)

export default service
